package com.huluobo.mvvm_base.mvvm.db;

import android.app.Application;

import androidx.room.Room;

import com.huluobo.mvvm_base.mvvm.bean.Money;

/**
 * Created by LC on 2024/2/27.
 * 数据库工具类,可以直接调用到数据库的增删改查方法
 */
public class AppDBUtils {
    private static final String DATA_BASE_NAME = "dou_yin.db";

    private static volatile AppDBUtils appDBUtils;

    private AppDatabase appDatabase;
    private UserDao userDao;

    private MoneyDao moneyDao;

    private AppDBUtils() {
    }

    public static AppDBUtils getInstance() {
        if (appDBUtils == null) {
            synchronized (AppDBUtils.class) {
                if (appDBUtils == null) {
                    appDBUtils = new AppDBUtils();
                }
            }
        }
        return appDBUtils;
    }

    public void init(Application application) {
        appDatabase = Room.databaseBuilder(application, AppDatabase.class, DATA_BASE_NAME)
                .allowMainThreadQueries()//允许主线程查询
                .build();
        userDao = appDatabase.getUserDao();
        moneyDao = appDatabase.getMoneyDao();
    }

    public UserDao getUserDao() {
        if (userDao == null) {
            userDao = appDatabase.getUserDao();
        }
        return userDao;
    }

    public MoneyDao getMoneyDao() {
        if (moneyDao == null) {
            moneyDao = appDatabase.getMoneyDao();
        }
        return moneyDao;
    }
}
